package proxy.cglib;

import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;

/**
 * @author yanyun zhu
 * @version 1.0
 * @date 2021/11/16 17:37
 *
 * https://www.jianshu.com/p/829e93528d56
 */
public class CglibTest {

    public static void main(String[] args) {
//        DaoProxy daoProxy = new DaoProxy();
//        Enhancer enhancer = new Enhancer();
//        enhancer.setSuperclass(Dao.class);  // 设置超类，cglib是通过继承来实现的
//        enhancer.setCallback(daoProxy);
//
//        Dao dao = (Dao)enhancer.create();   // 创建代理类
//        dao.update();
//        dao.select();



        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "/Users/zhanghao/Documents/toy/spring-framework-source-study/");
        Enhancer enhancer = new Enhancer();
        //继承被代理类
        enhancer.setSuperclass(UserDao.class);
        //设置回调
        enhancer.setCallback(new LogInterceptor());
        //设置代理类对象
        UserDao userDao = (UserDao) enhancer.create();
        //在调用代理类中方法时会被我们实现的方法拦截器进行拦截
        userDao.select();
    }

}
